import { _decorator, Node, Label, Prefab } from 'cc';
import { BaseView } from '../../../../scripts/libs/rxjs/cc3/BaseView';
import { Sprite } from 'cc';
import { ReactiveNumber } from '../../../../scripts/libs/rxjs/cc3/ReactiveNumber';
import { Colors, shapeInfoMap } from '../../game/basic';
import { MergeASAP } from '../../../../scripts/libs/rxjs/cc3/Merge';
import { ReactiveProperty } from '../../../../scripts/libs/rxjs/cc3/ReactiveProperty';
import { SpriteAtlas } from 'cc';
import { EditorStageView } from './EditorStageView';
import { UITransform } from 'cc';
import { gm } from '../../../../scripts/managers/GameManager';
import { instantiate } from 'cc';
const { ccclass, property, executeInEditMode } = _decorator;

@ccclass('EditorGlass')
@executeInEditMode
export class EditorGlass extends BaseView {

    posId = ReactiveProperty.Create<number>(null);

    glassId = ReactiveProperty.Create<number>(null);

    @property(ReactiveNumber)
    _shapeId: ReactiveNumber = ReactiveNumber.DefaultValue(0);
    @property
    get shapeId(): number { return this._shapeId.value }
    set shapeId(v: number) { this._shapeId.Value = v; }

    @property(Prefab)
    nailPrefab: Prefab = null;

    nails = ReactiveProperty.Create<number[]>(null);

    layer = ReactiveProperty.Create<number>(0);

    @property(SpriteAtlas)
    atlas: SpriteAtlas = null;

    @property(Node)
    shapeRoot: Node = null;

    @property(Node)
    nailRoot: Node = null;

    @property(Label)
    infoLabel: Label = null;

    useObserves() {
        return [
            this._shapeId.subscribe(this.refreshShape.bind(this)),
            MergeASAP(this.posId, this.glassId).subscribe(this.refresh.bind(this)),
            this.nails.subscribe(this.refreshNails.bind(this)),
            gm.editAltPressed.subscribe(_ => this.infoLabel.node.active = gm.editAltPressed.value),
        ];
    }

    refreshShape() {
        this.shapeRoot.destroyAllChildren();
        let shapeInfo = shapeInfoMap.get(this.shapeId);
        let center = shapeInfo.center;
        for (let i = 0; i < shapeInfo.data.length; i++) {
            let shapeItem = shapeInfo.data[i];
            let posInt = [shapeItem[0] - center[0], shapeItem[1] - center[1]];
            let spriteNode = new Node(`sprite_${i}`);
            spriteNode.setParent(this.shapeRoot);
            let pos = EditorStageView.getPosByOffsetRowCol(posInt[0], posInt[1]);
            spriteNode.setPosition(pos);
            let sprite = spriteNode.addComponent(Sprite);
            let [shapeTexName, rotNumStr] = shapeInfo.tex[i].split('|');
            sprite.spriteFrame = this.atlas.getSpriteFrame(`editor/glass_fb_${shapeTexName}`);
            spriteNode.setRotationFromEuler(0, 0, 90 * Number(rotNumStr));
            let transform = spriteNode.getComponent(UITransform)
            sprite.sizeMode = Sprite.SizeMode.CUSTOM;
            transform.width = EditorStageView.Space.x;
            transform.height = EditorStageView.Space.y;
            sprite.type = Sprite.Type.SLICED;
            sprite.trim = false;
        }
        this.refreshInfoPos();
        this.refreshNails();
    }

    refresh() {
        let infoText = `#${this.glassId.value}`;
        this.infoLabel.string = infoText;
    }

    refreshInfoPos() {
        let shapeInfo = shapeInfoMap.get(this.shapeId);
        let center = shapeInfo.center;
        if (shapeInfo.data.length > 0) {
            let dataItem = shapeInfo.data[0];
            let posInt = [dataItem[0] - center[0], dataItem[1] - center[1]];
            let pos = EditorStageView.getPosByOffsetRowCol(posInt[0], posInt[1]);
            this.infoLabel.node.setPosition(pos);
        }
    }

    refreshNails() {
        this.nailRoot.destroyAllChildren();
        let shapeInfo = shapeInfoMap.get(this.shapeId);
        if (this.nails.value != null) {
            for (let i = 0; i < shapeInfo.data.length; i++) {
                let shapeItem = shapeInfo.data[i];
                let nailColor = this.nails.value[i];
                if (nailColor > 0) {
                    let nailNode = instantiate(this.nailPrefab);
                    nailNode.getComponent(Sprite).color = Colors[nailColor];
                    nailNode.setParent(this.nailRoot);
                    let posInt = [shapeItem[0] - shapeInfo.center[0], shapeItem[1] - shapeInfo.center[1]];
                    let pos = EditorStageView.getPosByOffsetRowCol(posInt[0], posInt[1]);
                    nailNode.setPosition(pos);
                }
            }
        }

    }

}